package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;


public class MenuState implements State {

    private Library library;
    private ArrayList<State> menuStates;

    public MenuState(Library library) {
        this.library = library;
        buildMenuStates();
    }

    private void buildMenuStates() {
        menuStates = new ArrayList<State>();
        menuStates.add(new ExitState());
        menuStates.add(new ListBookState(library));
        menuStates.add(new CheckOutMenuState(library));
        menuStates.add(new MembershipDetailsState());
    }

    @Override
    public State next(BufferedReader reader) {
        try {
            String userInput = reader.readLine();
            int numericalInput=Integer.parseInt(userInput);
            return getState(numericalInput);
        } catch (IOException e) {
            return new ExitState();
        } catch (Exception e){
            return this;
        }

    }

    private State getState(int numericalInput) {
        return menuStates.get(numericalInput);
    }

    @Override
    public String getMessage() {
        return "Please Choose an Option:\n" +
                "0. Exit\n" +
                "1. List Books\n" +
                "2. Check Out\n" +
                "3. Membership Details";
    }
}
